package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.AgentAccountMapper;
import com.weixing.mall.provider.model.domain.AgentAccount;
import com.weixing.mall.provider.model.domain.AgentAccountBill;
import com.weixing.mall.provider.model.domain.AgentWithdraw;
import com.weixing.mall.provider.model.query.AgentAccountQuery;
import com.weixing.mall.provider.model.query.AgentBonusQuery;
import com.weixing.mall.provider.model.vo.AgentAccountVo;
import com.weixing.mall.provider.service.IAgentAccountBillService;
import com.weixing.mall.provider.service.IAgentAccountService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.service.IAgentWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 代理商模块-代理商账户表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentAccountServiceImpl extends ServiceImpl<AgentAccountMapper, AgentAccount> implements IAgentAccountService {
     @Autowired
     private IAgentService agentService;
     @Autowired
     private IAgentAccountBillService agentAccountBillService;
     @Autowired
     private IAgentWithdrawService agentWithdrawService;
    /**
     * 根据代理商ID查询代理商账户详情
     *
     * @param agentId
     * @return
     */
    @Override
    public AgentAccountVo selectByAgentId(long agentId) {
        //判定开关，如果不可体现是关闭状态，则返回前台值，前台显示为**号
        AgentAccountVo vo = baseMapper.selectByAgentId(agentId);
        if (true) {
            vo.setTxflag(1);
        }
        return vo;
    }

    @Override
    public AgentAccount findByAgentId(Long agentId) {
        return getOne(new QueryWrapper<AgentAccount>().eq("agent_id", agentId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAvaAmountByAgentId(List<AgentAccount> data, List<AgentAccountBill> aabList,List<AgentWithdraw> aw) {
        //封装待更新的数据
        /*List<AgentAccount> agentAccounts = Lists.newArrayList();
        data.forEach(awedd->{
            AgentVo agentVo =agentService.selectByAgentSn(awedd.getAgentSn());
            if(ObjectUtil.isNotEmpty(agentVo)&&agentVo.getId() != null){
                AgentAccount agentAccount = getOne(new LambdaQueryWrapper<AgentAccount>().eq(AgentAccount::getAgentId,agentVo.getId()));
                if(ObjectUtil.isNotEmpty(agentAccount) && awedd.getAmount() != null){
                    agentAccount.setAvaAmount(agentAccount.getAvaAmount().subtract(awedd.getAmount()));
                    agentAccounts.add(agentAccount);
                }
            }
        });*/

        //更新数据
        updateBatchById(data);
        agentAccountBillService.saveBatch(aabList);
        agentWithdrawService.saveBatch(aw);
    }

    @Override
    public IPage<AgentAccountVo> findByAdmin(Page page, AgentAccountQuery agentAccountQuery) {

        return baseMapper.findByAdmin(page,agentAccountQuery);
    }
}
